package com.key.web_admin.controller;

import com.key.common.base.RespResult;
import com.key.common.base.TranException;
import com.key.common.constants.Constants;
import com.key.common.utils.CosUtils;
import com.key.modules.dto.CustomerDTO;
import com.key.modules.entity.CustomerContract;
import com.key.modules.service.CustomerContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URL;

@RestController
@RequestMapping("/customerContract")
public class CustomerContractController {

    @Autowired
    CustomerContractService service;

    /**
     * 根据条件得到对应条件的客户合同信息
     * @param customerDTO
     * @return
     */
    @GetMapping("/showCustomerContract")
    public RespResult showCustomerContract(@RequestBody CustomerDTO customerDTO){
        return service.showCustomerContract(customerDTO);
    }

    /**
     * 删除客户合同，可多个
     * @param ids
     * @return
     */
    @PostMapping("/delCustomerContract")
    public RespResult delCustomerContract(Integer[] ids){
        return service.delCustomerContract(ids);
    }

    /**
     * 修改客户合同
     * @param contract
     * @return
     */
    @PostMapping("/updateCustomerContract")
    public RespResult updateCustomerContract(@RequestBody CustomerContract contract){
        return service.updateCustomerContract(contract);
    }

    /**
     * 上传文件
     * @param imgUrl
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public RespResult upload(@RequestParam("imgUrl") MultipartFile imgUrl) throws IOException {
        try {
            CosUtils.initCosClient();
            URL url = CosUtils.uploadFile(imgUrl, Constants.BUCKETNAME, CosUtils.formatKey(imgUrl));
            if(!url.toString().equals("")){
                return RespResult.success(url.toString());
            }else {
                throw new TranException("上传失败");
            }
        } finally{
            CosUtils.shutdownClient();
        }
    }

    /**
     * 添加客户合同
     * @param contract
     * @return
     */
    @PostMapping("/addCustomerContract")
    public RespResult addCustomerContract(@RequestBody CustomerContract contract){
        return service.addCustomerContract(contract);
    }

    /**
     * 根据id查询客户合同信息
     * @param id
     * @return
     */
    @GetMapping ("/showCustomerContractById")
    public RespResult showCustomerContractById(Integer id){
        return service.showCustomerContractById(id);
    }





}
